/*
 * @Author: ChunLai
 * @Date: 2022-06-07 09:25:50
 * @LastEditTime: 2025-05-28 11:55:32
 * @Description: 微信JSDK获取用户信息
 * @FilePath: \02.bldinsure\src\utils\wxJsSdk.js
 */
import $http from "@/utils/request";
import { formatUrlSearch } from "@/methods/format.js";
import store from "../store";
import { Toast } from "vant";
import { addUrlPara, copyTxt } from "@/utils/index.js";

const publicAppId = "wx7f77b13feef420d9";
export const isWxChat = () => {
  // console.log('isWxChat----');
  let res = false;
  let ua = window.navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == "micromessenger") {
    res = true;
  }
  return res;
};
export const wechatInitShareConfig = (
  shareUrl,
  ttValue = "",
  descValue = ""
) => {
  // console.log("wechatInitShareConfig----", shareUrl);
  if (!isWxChat()) {
    return;
  }
  const urlSearch = formatUrlSearch();
  $http
    .post("https://wxou.mschn.cn/wechart/getWxconfig", {
      url: `${window.location.origin}${window.location.pathname}${urlSearch}`,
    })
    .then(
      (res) => {
        if (res.result == 1) {
          let config = res.data;
          jWeixin.config({
            debug: false,
            appId: config.appId,
            timestamp: config.timestamp,
            nonceStr: config.nonceStr,
            signature: config.signature,
            jsApiList: [
              "onMenuShareAppMessage",
              "showMenuItems",
              "hideOptionMenu",
            ],
          });
          // 分享Api操作
          let baseConfig = store.state.home || {},
            shareMsg = baseConfig.shareInfo || {},
            iconUrl = `${shareMsg.icon || ""}`,
            shareTt = `${shareMsg.title || ""}`,
            shareDesc = `${shareMsg.desc || ""}`;
          const url_copy = addUrlPara("sharepage", "1", shareUrl);
          if (ttValue) {
            copyTxt(url_copy, () => {
              console.log("copyTxt");
              Toast("已复制链接,点击右上方分享");
            });
          }
          jWeixin.ready(function () {
            if (iconUrl && iconUrl.indexOf("http") === -1) {
              iconUrl = `https://wxou.mschn.cn/${shareMsg.icon || ""}`;
            }
            // 老版本分享Api
            jWeixin.onMenuShareAppMessage({
              title: `${ttValue || shareTt}`,
              desc: `${descValue || shareDesc}`,
              link: `${url_copy}`,
              imgUrl: `${iconUrl}`,
              success: function (res) {
                // 用户点击了分享后执行的回调函数
                console.log("onMenuShareAppMessage", res);
                // Toast({
                //   message: `分享成功,请联系客户尽快完成操作`,
                // });
              },
              cancel: function () {},
              fail: function () {},
            });
          });
        }
      },
      (err) => {}
    );
};

export const wxPageShare = (shareUrl, ttValue = "", descValue = "") => {
  // console.log("wxPageShare----", shareUrl);
  if (!isWxChat()) return;
  const urlSearch = formatUrlSearch();
  $http
    .post("https://wxou.mschn.cn/wechart/getWxconfig", {
      url: `${window.location.origin}${window.location.pathname}${urlSearch}`,
    })
    .then(
      (res) => {
        if (res.result == 1) {
          let config = res.data;
          wx.config({
            debug: false,
            appId: config.appId,
            timestamp: config.timestamp,
            nonceStr: config.nonceStr,
            signature: config.signature,
            jsApiList: ["onMenuShareAppMessage", "hideMenuItems"],
          });
          // 分享Api操作
          let baseConfig = store.state.home || {},
            shareMsg = baseConfig.shareInfo || {},
            iconUrl = `${shareMsg.icon || ""}`,
            shareTt = `${shareMsg.title || ""}`,
            shareDesc = `${shareMsg.desc || ""}`;
          const url_copy = addUrlPara("wxshare", "1", shareUrl);
          wx.ready(function () {
            if (iconUrl && iconUrl.indexOf("http") === -1) {
              iconUrl = `https://wxou.mschn.cn/${shareMsg.icon || ""}`;
            }
            wx.hideMenuItems({
              menuList: ["menuItem:copyUrl", "menuItem:share:timeline"],
            });
            // 老版本分享Api
            wx.onMenuShareAppMessage({
              title: `${ttValue || shareTt}`,
              desc: `${descValue || shareDesc}`,
              link: `${url_copy}`,
              imgUrl: `${iconUrl}`,
              success: function (res) {
                // console.log("onMenuShareAppMessage", res);
              },
            });
          });
        }
      },
      (err) => {}
    );
};
